package com.winit.projectPlan.listener;

import com.winit.common.CloudLinkSend.MqMessage;
import com.winit.common.CloudLinkSend.MsgSend;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.commons.util.ObjectToString;
import com.winit.projectPlan.manager.ProjectPlanMananger;
import com.winit.projectPlan.model.ProjectPlan;
import com.winit.projectPlan.repository.ProjectPlanRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@RabbitListener(queues = "uplan-queue")
public class QueueListener {
    private static Logger logger = LoggerFactory.getLogger(QueueListener.class);

    @Autowired
    private ProjectPlanMananger ppManager;
    @Autowired
    MsgSend msgSend;

    @Autowired
    private ProjectPlanRepository projectPlanRepository;

    @RabbitHandler
    public void process(MqMessage message) {
        logger.info("com.winit.projectPlan.listener.QueueListener.process " + ObjectToString.getString(message));
        Map map = message.getMap();
        ProjectPlan projectPlan=projectPlanRepository.findByUplanId((String) map.get("plan_id"));
        if(projectPlan==null){
            return;
        }
        switch (message.getType()) {
            case ONLINE:
//                上线
//                ppManager.dealUPlanOnline((String) map.get("plan_id"));
                projectPlan.setProjectPlanStatus("COMPLETED");
                projectPlanRepository.save(projectPlan);
                break;
            case DEL_PLAN:
                projectPlan.setProjectPlanIsDel("0");
//                删除
                ppManager.saveProjectPlan(projectPlan);
                break;
            case DEV_PLAN:
//                开发中
                projectPlan.setProjectPlanStatus("PLAN_DEVELOPING");
                projectPlanRepository.save(projectPlan);
                break;
            case INTEGRATE_PLAN:
//                进集成
                projectPlan.setProjectPlanStatus("INTEGRATIONING");
                projectPlanRepository.save(projectPlan);
                break;
            case CANCEL_INTEGRATE_PLAN:
                projectPlan.setProjectPlanStatus("PLAN_DEVELOPING");
                projectPlanRepository.save(projectPlan);
                break;
            case PROJECT:
                break;
            default:
                logger.error("com.winit.projectPlan.listener.QueueListener.process 参数错误" + message.getType());
                break;
        }
        msgSend.sendPlanMsg(message.getType(),map);
    }
}
